<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * CodeIgniter Invite Library
 *
 * @package         CodeIgniter
 * @subpackage      Libraries
 * @category        Libraries
 * @author          xiazhang
 * @license         MIT
 */
class Referrals {
    protected $ci;
    private $db;
    public function __construct() {
        $this->ci = & get_instance();
        $this->db = $this->ci->load->database('default',true);
    }

    //添加扫描二维码log
    public function addScanLog($array){
        $i = 1;
        $table = 'scan_log';
        do {
            $rs = $this->db->insert($table,$array);
        }while(!$rs && $i++ < 5);

        return $rs;
    }

	public function createInvite($slave,$openid){
        $i = 1;
        $table	= 'invite';
		$sdate	= date("Y-m-d",strtotime("-30 days"));
		$scan	= $this->getLastScan($openid,$sdate);
		if(!empty($scan)){
			$array	= array(
				'master'		=>	$scan['master'],
				'slave'			=>	$slave,
				'ip'			=>	get_ip(),
				'create_time'	=>	date("Y-m-d H:i:s"),
				'lastest_modify'=>	date("Y-m-d H:i:s"),
			);
			do {
				$rs = $this->db->insert($table,$array);
			}while(!$rs && $i++ < 5);
			
			$ticketConfig = $this->ci->config->item('ticketConfig');
			if($ticketConfig['active_invite']){
				//赠送体验券
				$this->ci->load->library('TicketClass');
				$add_arr  =  array(
					'uid'=> $scan['master'],
					'name' => 'ACCOUNT',//帐户
					'use_value' => 1,
					'source' => '90004',
					'object' => 'ticket',
					'add_time' => date("Y-m-d H:i:s"),
				);
				$this->ci->ticketclass->add($add_arr);
			}

			return $rs;
		}
        return false;
    }

    public function addInvite($slave){
        $uidArr = array(100496,100369);
        $rand  = rand(1,10000)%2;
        $master = $uidArr[$rand];
        $query = $this->db->select()->from("user_info")->where('uid', $slave)->limit(1)->get();
        if ($query->row()) {
            $slave_info = $query->row_array();
            $i = 1;
            $table  = 'invite';
            $array  = array(
                'master'        =>  $master,
                'slave'         =>  $slave,
                'ip'            =>  get_ip(),
                'create_time'   =>  $slave_info['create_time'],
                'lastest_modify'=>  $slave_info['create_time'],
            );
            do {
                $rs = $this->db->insert($table,$array);
            }while(!$rs && $i++ < 5);
            return $rs;
        }else{
            return false;
        }
        
    }

	public function getInvite(){
		$table = 'invite';
        $query = $this->db->select('master,slave')->from($table)->get();
        if ($query->result_array()) {
            return $query->result_array();
        } else {
            return false;
        }
    }

	public function getInviteAllVip(){
		$table = 'invite_vip';
        $query = $this->db->select()->from($table)->get();
        if ($query->result_array()) {
            return $query->result_array();
        } else {
            return false;
        }
    }

	public function getInviteVip($uid){
		$table = 'invite_vip';
		$data  = array('uid'=>$uid);
        $query = $this->db->select()->from($table)->where($data)->limit(1)->get();
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
    }

	public function getInviteByMaster($master){
		$table = 'invite';
		$data  = array('master'=>$master);
        $query = $this->db->select()->from($table)->where($data)->order_by('id','DESC')->get();
        if ($query->result_array()) {
            return $query->result_array();
        } else {
            return false;
        }
    }

	public function getInviteCnyByMaster($master){
		$table = 'invite';
		$data  = array('master'=>$master);
        $query = $this->db->select('sum(cny) as cny')->from($table)->where($data)->get();
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
    }

	public function getInviteDetails($where){
		$table = 'invite_details';
        $query = $this->db->select('sum(cny) as cny,sum(trade_cny) as trade_cny')->from($table)->where($where)->get();
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
    }

    public function getInviteBySlave($slave){
        $table = 'invite';
        $data  = array('slave'=>$slave);
        $query = $this->db->select()->from($table)->where($data)->order_by('id','DESC')->get();
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
    }

	public function getLastVipDate(){
		$table = 'invite_vip_details';
        $query = $this->db->select()->from($table)->order_by('edate','DESC')->limit(1)->get();
        if ($query->row()) {
			$row	=	$query->row_array();
            return $row['edate'];
        } else {
            return false;
        }
    }
	
	public function getEarn($where,$date){
		$table = 'invite_details';
		$this->db->where('date >=', $date);
		$this->db->order_by('date DESC,id DESC');
        $query = $this->db->select()->from($table)->where($where)->get();
        if ($query->result_array()) {
            return $query->result_array();
        } else {
            return false;
        }
    }

	private function getLastScan($openid,$sdate="",$edate=""){
		$table = 'scan_log';
		$data  = array('openid'=>$openid);

		if(!empty($sdate)){
			$this->db->where('create_time >=', $sdate);
		}

		if(!empty($edate)){
			$this->db->where('create_time <=', $edate);
		}
        $query = $this->db->select()->from($table)->where($data)->order_by('id','DESC')->limit(1)->get();
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
    }	
}

?>